using System;
using System.Collections.Generic;
using System.Text;
using Deepcode.Flux.Core.Extensions;

namespace Deepcode.Flux.Core.Systems.Asset
{
	public class AssetIcons
	{
		/// <summary>
		/// Cache members
		/// </summary>
		private AssetIcon [] _AllIconCache = null;
		private AssetIcon _DefaultIcon = null;
		/// <summary>
		/// Gets all of the icons associated with this installation
		/// </summary>
		/// <returns></returns>
		private AssetIcon [] GetAllIcons()
		{
			if( _AllIconCache != null ) return _AllIconCache;
			
			AddinTreeNode r = AddinTree.Tree.GetTreeNodeNoThrow("/Flux/Asset/Icon/");
			if( r == null ) return new AssetIcon[0];

			_AllIconCache = r.BuildChildItems(null).ToArray(typeof( AssetIcon )) as AssetIcon [];
			return _AllIconCache;
		}
		/// <summary>
		/// Gets the default icon
		/// </summary>
		/// <returns></returns>
		private AssetIcon GetDefaultIcon()
		{
			if( _DefaultIcon != null ) return _DefaultIcon;

			foreach( AssetIcon ai in GetAllIcons() )
				if (ai.Extension == "")
				{
					_DefaultIcon = ai;
					return _DefaultIcon;
				}

			return null;
		}
		/// <summary>
		/// Finds the appropriate icon file for the extension specified
		/// </summary>
		/// <param name="ext"></param>
		/// <returns></returns>
		public string GetIconFileForExtension( string ext )
		{
			ext = ext.ToUpper();

			foreach( AssetIcon ai in GetAllIcons() )
				if( ai.Extension == ext )
					return ai.IconFile;

			AssetIcon def = GetDefaultIcon();
			if( def != null ) return def.IconFile;
			return "";
		}
	}
}
